package cn.miui.mall.controller;

import cn.miui.mall.dao.imagedao;
import cn.miui.mall.pojo.product_details;
import cn.miui.mall.service.Detailsservice;
import cn.miui.mall.service.Typeservice;
import cn.miui.mall.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class HelloController
{
    @Autowired
    private Typeservice typeservice;
    @Autowired
    private Detailsservice detailsservice;
    @Autowired
    private Userservice userservice;
    @Autowired
    private imagedao imagedao;
    @RequestMapping("/")
    public ModelAndView login(){
       return new ModelAndView("redirect:/findAll");
    }
    @RequestMapping("/register.ftl")
    public ModelAndView register(){
        return new ModelAndView("register");
    }
    @RequestMapping("/login.ftl")
    public ModelAndView logins(){
        return new ModelAndView("login");
    }
    @RequestMapping("liebiao.ftl")
    public ModelAndView liebiao(Integer typeid, String name, Map<String, Object> map){
        map.put("typeList",detailsservice.findbytype(typeid));
        map.put("imageList",imagedao.findAll());
        map.put("name",name);
        return new ModelAndView("liebiao",map);
    }

    @RequestMapping("gouwuche.ftl")
    public ModelAndView gouwuche(){
        return new ModelAndView("gouwuche");
    }

    @RequestMapping("editCategory.ftl")
    public ModelAndView editCategory()
    {
        System.out.println("+++++++++++++++++++++++++++++");
        return new ModelAndView("admin/editCategory");
    }
    /***
     * 查询所有
     * @param map
     * @return
     */
    @RequestMapping("/findAll")
    public ModelAndView findAll(HttpServletRequest request,Map<String, Object> map, @RequestParam(value = "id", defaultValue = "0")Integer id){
        if (id!=0){
            map.put("userinfo",userservice.findbyId(id));
        }else{
            map.put("userinfo","");
        }
        List<product_details> detailsList=detailsservice.findAll();
        for (product_details details:detailsList) {
            imagedao.findBydetailsidAndType(details.getGoodsID(),"xiaotu");
        }
        map.put("imageList",imagedao.findAll());
        map.put("typesList",typeservice.findAll());
        map.put("detailsList",detailsservice.findAll());
        return new ModelAndView("index",map);
    }
}
